// 模型管理器
const { sequelize } = require('../utils/sequelize');

// 导入模型
const Goods = require('./Goods');
const GoodsAttributes = require('./GoodsAttributes');
const Seller = require('./Seller');
const UserAuth = require('./UserAuth');
const User = require('./User');

// 建立关联关系
// 商品和商品属性是一对多关系
Goods.hasMany(GoodsAttributes, {
  foreignKey: 'goods_id',
  as: 'attributes',
  onDelete: 'CASCADE'
});
GoodsAttributes.belongsTo(Goods, {
  foreignKey: 'goods_id',
  as: 'goods'
});

// 卖家和商品是一对多关系
Seller.hasMany(Goods, {
  foreignKey: 'seller_id',
  as: 'goods',
  onDelete: 'CASCADE'
});
Goods.belongsTo(Seller, {
  foreignKey: 'seller_id',
  as: 'seller'
});

// 用户和用户认证信息是一对多关系
User.hasMany(UserAuth, {
  foreignKey: 'user_id',
  as: 'auths',
  onDelete: 'CASCADE'
});
UserAuth.belongsTo(User, {
  foreignKey: 'user_id',
  as: 'user'
});

// 导出所有模型
module.exports = {
  sequelize,
  Goods,
  GoodsAttributes,
  Seller,
  UserAuth,
  User
};